package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.*;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.annotation.WebServlet;
import java.util.List;



public class SignOnServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String username;
    private String password;


//    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signon.jsp";
    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/login&register.jsp";
    private String Msg;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username = req.getParameter("username");
        System.out.println(username);
        this.password = req.getParameter("password");
        AccountService accountService = new AccountService();
//        String exc=req.getParameter("vcode");//获取前端用户填写的验证码
        HttpSession session =req.getSession();  //获取session
//        String excode=(String) session.getAttribute("session_code");  //获取后端checkcode随机验证码
//        //为防止验证码重复使用，session中的session_code一旦获得，就必须删除
//        session.removeAttribute("session_code");


        //校验用户名密码准确性，若不为空则判断是否可以登录
//        if(!(validate(exc,excode))){
//            req.setAttribute("signOnMsg",this.Msg);
//            req.getRequestDispatcher(SIGN_ON_FORM).forward(req,resp);
//        }else{
            //AccountService accountService = new AccountService();
            Account loginAccount = accountService.getAccount(username,password);
            if(loginAccount == null){
                this.Msg = "用户名或密码错误";
                req.setAttribute("signOnMsg",this.Msg);
                req.getRequestDispatcher(SIGN_ON_FORM).forward(req,resp);
            }else{
                session.setAttribute("loginAccount",loginAccount);
                if(loginAccount.isListOption()){
                    CatalogService catalogService = new CatalogService();
                    List<Product> myList = catalogService.getProductListByCategory(loginAccount.getFavouriteCategoryId());
                    session.setAttribute("myList",myList);
                }
                Cart cart = (Cart)session.getAttribute("cart");
                if(cart == null){
                    cart = new Cart();
                }
                cart = accountService.ReadMyCart(loginAccount,cart);
                String address2 = loginAccount.getAddress2();
                if(address2 == null || address2.equals("")){
                    loginAccount.setHasAddress2(false);
                }else{
                    loginAccount.setHasAddress2(true);
                }
                AddCart addCart = new AddCart();
                session.setAttribute("cart",cart);
                session.setAttribute("addCart",addCart);
                resp.sendRedirect("mainForm");
            }
//        }
    }

    private boolean validate(String exc,String excode){
        if(this.username == null || this.username.equals("")){
            this.Msg = "用户名不能为空";
            return false;
        }
        if(this.password == null || this.password.equals("")){
            this.Msg = "密码不能为空";
            return false;
        }
        if(exc == null || exc.equals("")){
            this.Msg = "请输入验证码";
            return false;
        }
        if(excode==null || !(excode.equalsIgnoreCase(exc))) {
            //忽略字母大小写，比较验证码
            //如果验证码正确，再比较用户的用户名和密码
            //验证码正确
            this.Msg = "验证码输入错误，请重新输入";
            return false;
        }
        return true;
    }
}
